package leetcode.editor.cn;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;

public class P435NonOverlappingIntervals {
    public static void main(String[] args) {
        Solution solution = new P435NonOverlappingIntervals().new Solution();
        System.out.println(solution.eraseOverlapIntervals(new int[][]{{1, 2}, {2, 3}}));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int eraseOverlapIntervals(int[][] intervals) {
            PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((o1, o2) -> {
                int cmp1 = o1[1] - o2[1];
                if (cmp1 == 0) {
                    return o2[0] - o1[0];
                }
                return cmp1;
            });
            for (int[] interval : intervals) {
                priorityQueue.add(interval);
            }
            int[] first = priorityQueue.poll();
            int min = first[0], max = first[1];
            int eraseNum = 0;
            while (!priorityQueue.isEmpty()) {
                int[] interval = priorityQueue.poll();
                int pre = interval[0];
                int next = interval[1];
                if (pre >= max) {
                    max = next;
                } else {
                    //存在交集就移除
                    eraseNum++;
                }
            }
            return eraseNum;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}